package com.tsingteng.cosfun.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.content.Context;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.ui.cosfun.cosinterface.Gesturelistener;
import com.tsingteng.cosfun.ui.cosfun.cosinterface.ObserverListener;
import com.tsingteng.cosfun.ui.cosfun.cosinterface.ObserverManager;

import java.util.Random;

public class HeartView extends RelativeLayout {
    private Context mContext;
    private long lastDown = 0;
    private long count = 0;
    float[] num = {-30, -20, 0, 20, 30};//随机心形图片角度
    private GestureDetector mGestureDetector;

    public HeartView(Context context) {
        super(context);
        initView(context);
    }

    public HeartView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public HeartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        mContext = context;
        mGestureDetector = new GestureDetector(mContext, new MyOnGestureListener());
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        ImageView imageView = new ImageView(mContext);
        LayoutParams params = new LayoutParams(100, 100);
        params.leftMargin = getWidth() - 200;
        params.topMargin = getHeight() / 2 - 300;
        imageView.setImageDrawable(getResources().getDrawable(R.drawable.great_pink_small_icon));
        imageView.setLayoutParams(params);
        imageView.setVisibility(View.INVISIBLE);
        addView(imageView);

        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "这里是点击爱心的动画，待展示", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mGestureDetector.onTouchEvent(event);
        // 一定要返回true，不然获取不到完整的事件
        return true;
    }

    public static ObjectAnimator scale(View view, String propertyName, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , propertyName
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator translationX(View view, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "translationX"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator translationY(View view, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "translationY"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator alpha(View view, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "alpha"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator rotation(View view, long time, long delayTime, float... values) {
        ObjectAnimator rotation = ObjectAnimator.ofFloat(view, "rotation", values);
        rotation.setDuration(time);
        rotation.setStartDelay(delayTime);
        rotation.setInterpolator(new TimeInterpolator() {
            @Override
            public float getInterpolation(float input) {
                return input;
            }
        });
        return rotation;
    }

    class MyOnGestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {


            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            if (heartViewLisenter != null) {
                heartViewLisenter.onLongClick();
            }
            super.onLongPress(e);
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return super.onScroll(e1, e2, distanceX, distanceY);
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return super.onFling(e1, e2, velocityX, velocityY);
        }

        @Override
        public void onShowPress(MotionEvent e) {
            super.onShowPress(e);
        }

        @Override
        public boolean onDown(MotionEvent e) {
            return super.onDown(e);
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            return super.onDoubleTap(e);
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            final ImageView imageView = new ImageView(mContext);
            LayoutParams params = new LayoutParams(300, 300);
            params.leftMargin = (int) e.getX() - 150;
            params.topMargin = (int) e.getY() - 300;
            imageView.setImageDrawable(getResources().getDrawable(R.drawable.great_pink_small_icon));
            imageView.setLayoutParams(params);
            addView(imageView);

            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.play(scale(imageView, "scaleX", 2f, 0.9f, 100, 0))
                    .with(scale(imageView, "scaleY", 2f, 0.9f, 100, 0))
                    .with(rotation(imageView, 0, 0, num[new Random().nextInt(4)]))
                    .with(alpha(imageView, 0, 1, 100, 0))
                    .with(scale(imageView, "scaleX", 0.9f, 1, 50, 150))
                    .with(scale(imageView, "scaleY", 0.9f, 1, 50, 150))
                    .with(translationY(imageView, 0, -600, 800, 400))
                    .with(alpha(imageView, 1, 0, 300, 400))
                    .with(scale(imageView, "scaleX", 1, 3f, 700, 400))
                    .with(scale(imageView, "scaleY", 1, 3f, 700, 400));

            animatorSet.start();
            animatorSet.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    removeViewInLayout(imageView);

                }
            });
            if (heartViewLisenter != null) {
                heartViewLisenter.onMoreClick();
            }
            // ObserverManager.getInstance().notifyObserver(ObserverListener.EVENT_ZAN_ADD, 0, 0);
            return true;
//            return super.onDoubleTapEvent(e);
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            //ObserverManager.getInstance().notifyObserver(ObserverListener.EVENT_VIDEO_PLAY, 0, 0);
            if (heartViewLisenter != null) {
                heartViewLisenter.onSingleClick();
            }
            return true;
        }

        @Override
        public boolean onContextClick(MotionEvent e) {
            return super.onContextClick(e);
        }
    }

    HeartViewLisenter heartViewLisenter = null;

    public void setOnViewClickLisenter(HeartViewLisenter heartViewLisenter) {
        this.heartViewLisenter = heartViewLisenter;
    }

    public interface HeartViewLisenter {
        void onMoreClick();

        void onSingleClick();
        void onLongClick();
    }
}
